#include "InteractTextboxList.h"



InteractTextboxList::InteractTextboxList(void){
}


InteractTextboxList::~InteractTextboxList(void){
	SDL_FreeSurface(screen);
	SDL_Quit();
}

void InteractTextboxList::run(){
bool quit = false;
	const int WINDOW_WIDTH = 800;
	const int WINDOW_HEIGHT = 600;
	const char* WINDOW_TITLE = "Sample Interaction";
	
	// initialise the screen
	SDL_Init( SDL_INIT_VIDEO );
	TTF_Init();
	screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 32, 
      SDL_HWSURFACE | SDL_DOUBLEBUF );
	SDL_WM_SetCaption( WINDOW_TITLE, 0 );

	// create a Listbox 
    
	
	/*LISTSTR* unaLista = new LISTSTR();
	unaLista->push_back("e1");
	unaLista->push_back("e2");
	unaLista->push_back("e3");
	unaLista->push_back("e4");
	unaLista->push_back("e5");
	
	ListTextBox *listbox = new ListTextBox( screen, 4,10,10,300,20, unaLista);*/
	
	ListTextBox *listbox = new ListTextBox( screen, 4,10,10,300,20);
	//Agregando elementos
	/*listbox->addOption("e1");
	listbox->addOption("e2");
	listbox->addOption("e3");
	listbox->addOption("e4");
	listbox->addOption("e5");
	listbox->addOption("e7");
	listbox->addOption("e9");*/

	ScrollButtonDown* buttonD = new ScrollButtonDown(screen,310,70,20,20,listbox);
	ScrollButtonUp* buttonU = new ScrollButtonUp(screen,310,10,20,20,listbox);
	Textbox * unaTextbox = new Textbox(screen,10,150,300,20);
	Button * aButton = new Button(screen, 330,150,60,20,0xFFFF99 ,"Crear");
	Button * bButton = new Button(screen, 330,180,60,20,0xFFFF99 ,"Borrar");
	listbox->draw(); // draw the listbox
	buttonD->draw();
	buttonU->draw();
	aButton->draw();
	bButton->draw();
	unaTextbox->drawBox();
	unaTextbox->drawText();
	
 
   SDL_Event event;
    while(quit == false) {
        SDL_Delay(50);
        if(SDL_PollEvent(&event)) {
			
            listbox->handleEvent(&event); // pass the event to box1            
			buttonD->handleEvent(&event);
			buttonU->handleEvent(&event);
			unaTextbox->handleEvent(&event);
			if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN) || (aButton->handleEvent(&event) == true) ) {
				std::string tmp = unaTextbox->getText();
				unaTextbox->clearBox();
				if(!tmp.empty())
					listbox->addOption(tmp);			
			}
			if( bButton->handleEvent(&event) == true)  {
				listbox->deleteOption();
			}
			listbox->draw(); // draw the box
			buttonD->draw();
			buttonU->draw();
			aButton->draw();
			bButton->draw();
			unaTextbox->draw();
 
			if( event.type == SDL_QUIT ){
			//Quit the program
			quit = true;
			}
		}
        SDL_Flip(screen);
    }





}